package hongcaosp.app.android.contact.friends;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.comment.LoadMoreHandler;
import hongcaosp.app.android.common.BaseAdapter;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.common.IListView;
import hongcaosp.app.android.modle.bean.IUserDetail;
import hongcaosp.app.android.user.qrcode.QRScanActivity;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.view.ProxyAdapter;

public class FindFriendActivity extends BaseActivity implements IListView<IUserDetail> {

    private RecyclerView recyclerView;

    private BaseAdapter<IUserDetail> adapter;
    private FindFriendPresenter findFriendPresenter;
    private LoadMoreHandler loadMoreHandler;

    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);
        recyclerView = findViewById(R.id.recycler_view);
        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        findViewById(R.id.scan_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QRScanActivity.toActivity(FindFriendActivity.this);
            }
        });

        adapter = new BaseAdapter<IUserDetail>() {
            @NonNull
            @Override
            public BaseHolder<IUserDetail> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return new FriendHolder(LayoutInflater.from(FindFriendActivity.this).inflate(R.layout.item_friend,parent,false), FindFriendActivity.this);
            }
        };

        View header = getLayoutInflater().inflate(R.layout.item_friend_check,null);

        header.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LocalFindFriendActivity.toActivity(FindFriendActivity.this);
            }
        });

        ProxyAdapter proxyAdapter =new ProxyAdapter(adapter);
        proxyAdapter.addHeaderView(header);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayout.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(proxyAdapter);

        recyclerView.addOnScrollListener(loadMoreHandler = new LoadMoreHandler(linearLayoutManager) {
            @Override
            public void loadMore() {
                findFriendPresenter.loadMore();
            }
        });

        findFriendPresenter = new FindFriendPresenter(this);
        findFriendPresenter.freshData();
    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_findfriend;
    }

    @Override
    public void freshList(List<IUserDetail> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        adapter.setData(data);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void addList(List<IUserDetail> data, boolean canLoadMore) {
        loadMoreHandler.loadMoreFinish(canLoadMore);
        adapter.addData(data);
        adapter.notifyDataSetChanged();
    }
}
